<?
require_once('functions.php');

function GetUsers()
{
        $conn = GetConnection();
		return $conn->query('SELECT * FROM Customers');
        //return $conn->query('SELECT U.*, K.Name FROM 2012UG_Users U Join 2012UG_Keywords K ON U.Keyword_id = K.id');
}
function BlankUser()
{
        return array('FirstName'=>null,'LastName'=>null,'created_at'=>null,'updated_at'=>null,'id'=>null, 'Address1'=>null, 'Address2'=>null, 'City'=> null, 
		'State'=> null, 'ZipCode'=> null, 'Country'=> null);
}
function GetUser($id)
{
        $conn = GetConnection();
        $sql = "SELECT * FROM Customers WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}
function GetMethod($id, $table)
{
	    $conn = GetConnection();
        $sql = "SELECT * FROM $table WHERE Customers_id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $pay = $results;
        $conn->close();
        return $pay;
}
function SaveUser(&$row)
{
        $conn = GetConnection();
        $row2 = EscapeRow($row, $conn);
        if(empty($row['id']))
        {
                $sql =  "INSERT INTO Customers (FirstName, LastName, Address1, Address2, City, State, Country, ZipCode, AccessLevel_id,created_at)
                VALUES ('$row[FirstName]', '$row[LastName]', '$row[Address1]', '$row[Address2]',
                       '$row[City]', '$row[State]', '$row[Country]', '$row[ZipCode]', '1', NOW()) " ;
        }else{
                $sql =  "UPDATE Customers "
                .       "Set FirstName='$row2[FirstName]', LastName='$row2[LastName]',
                                 created_at='$row2[created_at]', updated_at='$row2[updated_at]', Address1='$row2[Address1]',
                                 Address2='$row2[Address2]', City='$row2[City]', State='$row2[State]', 
                                 Country='$row2[Country]', ZipCode='$row2[ZipCode]' "
                .       "WHERE id=$row2[id] ";  
        }
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        if(empty($row['id']) && empty($error))
        {
                $row['id'] = $conn->insert_id;
        }
        $conn->close();
        
        return $error == '' ? true : array('SQL Error' => $error);
}
function DeleteUser($id)
{
        $conn = GetConnection();
        $sql =  "DELETE FROM Customers "
                .       "WHERE id=$row[id]";
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        $conn->close();
        
        return $error == '' ? true : array('SQL Error' => $error);
}
function ValidateUser($row)
{
        $errors = array();
        if(empty($row['FirstName'])) $errors['FirstName'] = 'First Name is required';
        if(empty($row['LastName'])) $errors['LastName'] = 'Last Name is required';
        if(empty($row['Address1'])) $errors['Address1'] = 'Keyword_id is required';

        
        return count($errors) == 0 ? true : $errors;
}
